/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年4月15日
 * @version 0.1
 */
package com.suredy.config.ctrl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.config.model.SN;
import com.suredy.config.service.SNSrv;
import com.suredy.core.ctrl.BaseCtrl;


/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value="config")
public class SnModeCtrl extends BaseCtrl {
	private final static Logger log = LoggerFactory.getLogger(SnModeCtrl.class);
	private final String MSG_SNMODE_NOT_FOUND = "未能获取编号模式属性数据，请查看log获取更多错误信息！";
	@Autowired
	private SNSrv service;

	/**
	 * get snmode list
	 * 
	 * @param type
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "snmode-list")
	public ModelAndView getAll() {
		ModelAndView view = new ModelAndView("config/snmode-list");
		List<SN> data = this.service.getAll();
		view.addObject("data", data);
		return view;
	}

	/**
	 * get snmode form
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("snmode-form")
	public ModelAndView get(String id) {	
		ModelAndView view = new ModelAndView("config/snmode-form");
		if (StringUtils.isBlank(id)) {
			log.info("Parameter String[id] is blank, try to create new serial number mode.");
			return view;
		} 

		SN data = this.service.get(id);
		if (data == null) {
			log.error("Can not get serial number mode data by id[" + id + "].");
			return this.gotoErrorPage(this.MSG_SNMODE_NOT_FOUND);
		}
		view.addObject("data", data);
		
		return view;
	}
	
	@RequestMapping("snmode-save")
	@ResponseBody
	public String save(HttpServletRequest request) {
		String id = request.getParameter("id");
		String content = request.getParameter("content");
		if (StringUtils.isBlank(content)) {
			log.error("Parameter String[name/path] is blank.");
			return null;
		}
		SN n = new SN();
		if (id != null){
			n.setId(id);
		}
		n.setContent(content);
		n = this.service.saveOrUpdate(n);

		return n == null ? null : n.getId();
	}

	
}